<?php if(! defined('HAS_POWER') ) exit('无权访问!');
/**
==============================================================================
 * Class Route GracePHP的路由控制类（目前只实现pathinfo模式）
 * @author 肖永标
 * @date 2014-11-30
==============================================================================
 */
class Route {

    /**
     * 目前的实现 index.php/controller/action/[key=value...]
     */
	public static function run()
	{
        if($_GET['create'] == 'do')
        {
            $create = new GraceCreate();
            if($create->isLogin())
            {
                $create->run();
            }
            else
            {
                $create->login();
            }
        }
		else if(Config::getItem('pathInfo'))
		{
            if(isset($_SERVER['PATH_INFO']))
            {
                $pathInfo = $_SERVER['PATH_INFO'];
                $pathInfoArr = explode('/', $pathInfo);

                if(empty($pathInfoArr[1]))
                    $controller = Config::getItem('defaultController') . 'Controller';
                else
                    $controller = ucfirst($pathInfoArr[1]) . 'Controller';

                if(strpos($pathInfoArr[2], '.') !== false)
                    $pathInfoArr[2] = substr($pathInfoArr[2], 0, strpos($pathInfoArr[2], '.'));

                $action = $pathInfoArr[2];

                $queryParam = array_slice($pathInfoArr, 3);

                if (count($queryParam) % 2 != 0)
                    throw new Exception('键值对必须要整对出现!');

                $sum = count($queryParam) / 2;
                // 载入全局get中
                for($i=0; $i<$sum; $i++)
                {
                    $temp = array_slice($queryParam, $i*2, 2);
                    if(strpos($temp[1], '.') !== false)
                        $temp[1] = substr($temp[1], 0, strpos($temp[1], '.'));
                    $_GET[$temp[0]] = htmlspecialchars(addslashes($temp[1]));
                }

                eval('$controllerClass = new $controller();');

                if(empty($action))
                    $controllerClass->index();
                else
                    $controllerClass->$action();
            }
            else
            {
                $controller = Config::getItem('defaultController') . 'Controller';
                eval('$controllerClass = new $controller();');
                $controllerClass->index();
            }
		}
        else
        {
            if(isset($_GET['c']))
            {
                $name =  $_GET['c'] . 'Controller';
                $controller = new $name();
                if(isset($_GET['a']))
                {
                    $action = $_GET['a'];
                    $controller->$action();
                }
                else
                {
                    $controller->index();
                }

            }
            else
            {
                $controller = Config::getItem('defaultController') . 'Controller';
                eval('$controllerClass = new $controller();');
                $controllerClass->index();
            }
        }


	}
}